package com.kcht.itsed.mws.devicescheduler.biz;

import com.shhic.itsed.mws.deviceintf.RawTagOperationResult;
import com.kcht.itsed.mws.devicescheduler.facade.IRawTagOperationResultSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.jms.*;
import java.util.List;

/**
 * 本地JMSQueu的标签结果发送
 * <p>
 * 后续如果扩展远程JMS Queue则再搞一个实现
 *
 * @author 张文硕
 */
@Component
public class LocalRawTagOperationResultSender implements IRawTagOperationResultSender {
    @Autowired
    @Qualifier("queueJmsTemplate")
    private JmsTemplate queueJmsTemplate;
    @Autowired
    @Qualifier("originalTagResult")
    private Queue originalTagResultQueue;    //原始标签操作结果接收队列


    private class RawTagOperationResultSender {
        public void send(RawTagOperationResult record) {
            queueJmsTemplate.send(originalTagResultQueue,
                    session -> session.createObjectMessage(record));
        }
    }

    private abstract class RawTagOperationResultMessageProcessor {
        public void process() {
            doMessageSend(new RawTagOperationResultSender());
        }

        //由子类重写，真正要发送的东西
        public abstract void doMessageSend(RawTagOperationResultSender sender);
    }

    @Override
    public void send(List<? extends RawTagOperationResult> tagOpResList) {
        new RawTagOperationResultMessageProcessor() {
            @Override
            public void doMessageSend(RawTagOperationResultSender sender) {
                for (RawTagOperationResult tr : tagOpResList) sender.send(tr);
            }
        }.process();
    }

    @Override
    public void send(RawTagOperationResult tagOpRes) {
        new RawTagOperationResultMessageProcessor() {
            @Override
            public void doMessageSend(RawTagOperationResultSender sender) {
                sender.send(tagOpRes);
            }
        }.process();
    }

}
